import { defineStore } from 'pinia';
import { getToken, removeToken, setToken } from '../utils/token-utils';
import type { UserInfoState } from './interface';
import {ElMessage} from 'element-plus'
import {staticRoutes} from '@/router/routes'
import {getLogin,getUserInfo,postlogout} from '@/api/user'

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */


export const useUserInfoStore = defineStore('userInfo', {
    state(){
        return{
          token:localStorage.getItem("token_key") || "",
          userInfo:{
            routes:[],
            buttons:[],
            roles:[],
            name:"",
            avatar:""
          },
          menuRoutes: staticRoutes,
        }
    },
    actions:{
      async loginAsync({username, password}){
        // console.log(username, password,'55');
        const result =  await getLogin({username, password})
        // console.log(result,'result');
        this.token = result.token;
        localStorage.setItem("token_key",result.token)
        
      },

      async getUserInfo(){
        const result = await getUserInfo()
        console.log(result,'getUserInfo');
        this.userInfo = result
        
      },

      reset(){
        this.token = "",
        localStorage.removeItem("token_key")
        this.userInfo={
          routes:[],
          buttons:[],
          roles:[],
          name:"",
          avatar:""
        }
      },

      async postlogoutAsync(){
        await postlogout()
        this.reset()

      }

    },
    getters:{}
});
